package com.rd.student.jike.class2.xuezhixin.Work02;

import java.util.*;

public class Test {
    public static void main(String[] args) {

        //1、打印出雪花形状的金字塔2
//        Work01.work2_1();

        //2、打印出数字逐渐递增的金字塔
//        Work01.work2_2();

        //3、从键盘输入一个整数（1~20），则以该数字为矩阵的大小，把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
//        Work01.work2_3();

        //4、产生10个1-100的随机数，并放到一个集合中，把集合中大于等于10的数字放到另一个集合中，打印到控制台。
//        Work01.work2_4();

        //5.数组中的所有qq号存放在集合中，同时将重复元素删除，将最终结果打印出来。
//        Work01.work2_5();

        //6、分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序，如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序。
//        Work01.work2_6();

        //7.将第一个数组元素作为key，第二个数组元素作为value存储到Map集合中。
//        Work01.work2_7();

        //8.创建Score类，属性包括id[1-40]，scroe[0-100]，id逐个递增，分数随机生成。
        // 创建集合保存20个对象，分别打印出分数最高与最低的学生id及分数，并计算出分数的平均值，
        // 将分数在60分以上（包括60分）的id保存到一起中并打印，将分数在60分以下的id保存在一起并打印。
//        Work01.work2_8();

        //9.已知有十六支男子足球队参加2008北京奥运会。
        // 写一个程序，使用集合把这16 支球队随机分为8组，每组两只球队随机一个胜负关系，
        // 比如0代表1队赢，1代表2队赢，将获胜的8支球队再次随机分为4组，同样随机出胜负关系后，将4支获胜球队随机分为2组，
        // 最后随机两支获胜球队的胜负关系，筛选出冠军球队。打印输出比赛的分组情况、获胜情况和最终的冠军。
//        Work01.work2_9();

        //10.罗马数字
//        Work01.work2_10();

        //11.井字棋
//        Work01.work2_11();

        //12.五子棋
//        Work01.work2_12();
    }
}

class Work01{

    //1、打印出雪花形状的金字塔
    public static void work2_1(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("您想制作的金字塔层数");
        int floor = scanner.nextInt();

        for(int i = 1;i <= floor;i++){
            for(int j = floor-i;j >= 0;j--){
                System.out.print(" ");
            }
            for(int j = 0;j < 2*i-1 ;j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    //2、打印出数字逐渐递增的金字塔
    public static void work2_2(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("您想制作的金字塔层数");
        int floor = scanner.nextInt();

        for(int i = 1;i<=floor;i++){
            for (int j = floor - i;j>=0;j--){
                System.out.print("  ");
            }
            for(int j = i;j>0;j--){
                System.out.print(j + " ");
            }
            for(int j = 2;j <= i;j++){
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }

    //3、从键盘输入一个整数（1~20），则以该数字为矩阵的大小，把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
    public static void work2_3(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("您想做的矩形大小");
        int n = scanner.nextInt();

        int[][] a= new int[n][n];
        int s = 0,x = n-1,l = 0,r = n-1;
        int num = 1;
        while (true){
            for(int i = l;i <= r;i++){
                a[s][i] = num;
                num++;
            }
            s++;
            for(int i = s;i <= x;i++){
                a[i][r] = num;
                num++;
            }
            r--;
            for(int i = r;i >= l;i--){
                a[x][i] = num;
//                if(num == n*n){
//                    break;
//                }
                num++;
            }
            x--;
            for (int i = x;i >= s;i--){
                a[i][l] = num;
                num++;
            }
            l++;
            if(num > n*n){
                break;
            }
        }
//        if(n % 2 !=0){
//            a[s][l] = num;
//        }
        for(int i = 0;i<a.length;i++){
            for(int j = 0;j<a[i].length;j++){
                System.out.print(a[i][j] + "\t");
            }
            System.out.println();
        }
    }

    //4、产生10个1-100的随机数，并放到一个集合中，把集合中大于等于10的数字放到另一个集合中，打印到控制台。
    public static void work2_4(){
        ArrayList arrayList = new ArrayList<>();
        ArrayList arrayList1 = new ArrayList<>();
        for(int i = 0;i<10;i++){
            int num = (int)(Math.random()*100);
            arrayList.add(num);
            if(num >= 10){
                arrayList1.add(num);
            }
        }
        System.out.println("集合中所有元素为：");
        for (int i = 0;i<arrayList.size();i++){
            System.out.print(arrayList.get(i) + "  ");
        }
        System.out.println();
        System.out.println("集合中所有大于等于10 的元素为:");
        for (int i = 0;i<arrayList1.size();i++){
            System.out.print(arrayList1.get(i) + "  ");
        }
    }

    //5.数组中的所有qq号存放在集合中，同时将重复元素删除，将最终结果打印出来。
    public static void work2_5(){
        String[] strs = {"12345","67891","123478099","9876543","67891","12347809933","12809933","12347809933111","9876543","12809933"};

        HashMap hashMap = new HashMap<>();
        for (int i = 0;i<strs.length;i++){
            if(strs[i].length() <= 9 && strs[i].length() >= 6){
                hashMap.put(strs[i],0);
            }
        }

        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            System.out.print(key + "\t");
        }
    }

    //6、分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序，如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序。
    public static void work2_6(){
        ArrayList arrayList = new ArrayList<>();
        arrayList.add(new Student2_6("liusan ",20,90));
        arrayList.add(new Student2_6("lisi ",22,90));
        arrayList.add(new Student2_6("wangwu ",20,99));
        arrayList.add(new Student2_6("sunliu ",22,100));

        //   Comparable
//        Collections.sort(arrayList);
//        System.out.println(arrayList);

        //Comparator
        Collections.sort(arrayList, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                Student2_6 s1 = (Student2_6) o1;
                Student2_6 s2 = (Student2_6) o2;
                if(s1.getAge() > s2.getAge()){
                    return -1;
                } else if (s1.getAge() < s2.getAge()) {
                    return 1;
                }else {
                    if (s1.getGrade() >s2.getGrade()){
                        return -1;
                    } else if (s1.getGrade() <s2.getGrade()) {
                        return 1;
                    }else {
                        return 0;
                    }
                }
            }
        });
        System.out.println(arrayList);
    }

    //7.将第一个数组元素作为key，第二个数组元素作为value存储到Map集合中。
    public static void work2_7(){
        String[] str1 = {"黑龙江省","黑龙江省","黑龙江省","黑龙江省","浙江省","浙江省","江西省","广东省","福建省","福建省"};
        String[] str2 = {"哈尔滨","大庆","绥化","齐齐哈尔","杭州","宁波","南昌","广州","福州","厦门"};

        HashMap hashMap = new HashMap<>();
        for(int i = 0;i<str1.length;i++){
            String key = str1[i];
            if(hashMap.containsKey(key)){
                String value = (String) hashMap.get(key);
                String str = value + "," + str2[i];
                hashMap.put(str1[i],str);
            }else {
                hashMap.put(str1[i],str2[i]);
            }
        }

        StringBuffer stringBuffer = new StringBuffer();
        String resurt = "";
        Set set = hashMap.keySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            String key = (String) iterator.next();
            String value = (String) hashMap.get(key);
            stringBuffer.append(key);
            resurt =stringBuffer.toString();
            System.out.println("{" + key + "=" + value + "}");
        }
        System.out.println(resurt);

        for(int i = 0;i<str1.length;i++){
            int num = 1;
            if(!str1[i].equals("0")){
                for(int j = i+1;j<str1.length;j++){
                    if(str1[i].equals(str1[j])){
                        str1[j] = "0";
                        num++;
                    }
                }
                System.out.println(str1[i] + "counts:" + num);
            }
        }
    }

    //8.创建Score类，属性包括id[1-40]，scroe[0-100]，id逐个递增，分数随机生成。
    // 创建集合保存20个对象，分别打印出分数最高与最低的学生id及分数，并计算出分数的平均值，
    // 将分数在60分以上（包括60分）的id保存到一起中并打印，将分数在60分以下的id保存在一起并打印。
    public static void work2_8(){
        ArrayList arrayList = new ArrayList<>();//20个数据
        ArrayList arrayList1 = new ArrayList<>();//成绩大于60的
        ArrayList arrayList2 = new ArrayList<>();//成绩小于60的
        int gradeAll = 0;//总分
        for(int i = 0;i<20;i++){
            arrayList.add(new Score());
        }
        for(int i = 0;i<arrayList.size();i++){
            Score score = (Score) arrayList.get(i);
            gradeAll += score.getGrade();
            if(score.getGrade() >= 60){
                arrayList1.add(score);
            }else {
                arrayList2.add(score);
            }
        }
        Collections.sort(arrayList);
        Score max = (Score) arrayList.get(arrayList.size()-1);
        System.out.println("最高分为：" + max);
        Score min = (Score) arrayList.get(0);
        System.out.println("最低分为：" + min);
        System.out.println("平均分为：" + gradeAll/arrayList.size());
        System.out.println("成绩大于60的");
        System.out.println(arrayList1);
        System.out.println();
        System.out.println("成绩小于60的");
        System.out.println(arrayList2);
    }


    //9.已知有十六支男子足球队参加2008北京奥运会。
    // 写一个程序，使用集合把这16 支球队随机分为8组，每组两只球队随机一个胜负关系，
    // 比如0代表1队赢，1代表2队赢，将获胜的8支球队再次随机分为4组，同样随机出胜负关系后，将4支获胜球队随机分为2组，
    // 最后随机两支获胜球队的胜负关系，筛选出冠军球队。打印输出比赛的分组情况、获胜情况和最终的冠军。
    public static void work2_9() {
        String[] str = {"科特迪瓦", "阿根廷", "澳大利亚", "塞尔维亚", "荷兰", "尼日利亚", "日本", "美国", "中国", "新西兰", "巴西", "比利时", "韩国", "喀麦隆", "洪都拉斯", "意大利"};
        int l =(int) (Math.log(str.length)/Math.log(2));
        for (int a = 1; a <= l; a++) {
            String[] str1 = new String[str.length/2];
            int[] num = new int[str.length];
            for (int j = 0; j < num.length; j++) {
                num[j] = j;
            }
            for (int i = 0; i < str.length / 2; i++) {
                int num1 = (int) (Math.random() * 2);
                int c1 = 0;
                int c2 = 0;
                for (int j = 0; j < 1; j++) {
                    c1 = (int) (Math.random() * (str.length));
                    if (num[c1] == -1) {
                        j = -1;
                    }
                }
                num[c1] = -1;
                for (int j = 0; j < 1; j++) {
                    c2 = (int) (Math.random() * (str.length));
                    if (num[c2] == -1) {
                        j = -1;
                    }
                }
                num[c2] = -1;
                if (num1 == 0) {
                    System.out.println(str[c1] + "  对战  " + str[c2] + "\t\t" + str[c1] + " 胜");
                    str1[i] = str[c1];
                } else {
                    System.out.println(str[c1] + "  对战  " + str[c2] + "\t\t" + str[c2] + " 胜");
                    str1[i] = str[c2];
                }
            }
            str = str1;
            if(a == l ){
                System.out.println("最终的胜者为：" + str[0]);
            }else {
                System.out.println("第" + a + "轮结束!!!" + "\t" + "开始第" + (a+1) + "轮");
                System.out.println();
            }
        }
    }


    //10.罗马数字
    public static void work2_10(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要转换的罗马数字:");
        String str = scanner.next();
        int[] a = new int[str.length()];
        for (int i = 0;i<str.length();i++){
            switch (str.charAt(i)){
                case 'I':
                    a[i] = 1;
                    break;
                case 'V':
                    a[i] = 5;
                    break;
                case 'X':
                    a[i] = 10;
                    break;
                case 'L':
                    a[i] = 50;
                    break;
                case 'C':
                    a[i] = 100;
                    break;
                case 'D':
                    a[i] = 500;
                    break;
                case 'M':
                    a[i] = 1000;
                    break;
                default:
                    System.out.println("您输入的罗马数字有错");
            }
        }

        int num = 0;
        for(int i = 0;i<a.length;i++){
            if(i == a.length-1){
                num += a[i];
                break;
            }
            if(a[i] >= a[i+1]){
                num += a[i];
            } else if (a[i] < a[i+1]) {
                num -= a[i];
            }
        }
        System.out.println("转为阿拉伯数字为：" + num);
    }


    //11.井字棋游戏
    public static void work2_11(){
        int[][] moves = new int[9][2];
        char[][] broad = new char[3][3];
        for (int i = 0;i<broad.length;i++){
            for (int j = 0;j<broad[i].length;j++){
                broad[i][j] = ' ';
            }
        }

        Scanner scanner = new Scanner(System.in);
        for(int i = 0;i< moves.length;i++){
            System.out.println("您要下在第几列：");
            moves[i][0] = scanner.nextInt()-1;
            if(moves[i][0] >= 3){
                System.out.println("棋盘只有3列");
                continue;
            }
            System.out.println("您要下在第几行：");
            moves[i][1] = scanner.nextInt()-1;
            if(moves[i][1] >= 3){
                System.out.println("棋盘只有3行");
                continue;
            }
            if(broad[moves[i][1]][moves[i][0]] == ' '){
                broad[moves[i][1]][moves[i][0]] = i % 2 == 0?'X':'O';
            }else {
                System.out.println("这个位置已经下过");
                i -= 1;
            }

            //遍历
            for (int a = 0;a<broad.length;a++){
                for (int b = 0;b<broad[a].length;b++){
                    System.out.print(broad[a][b] + "\t");
                }
                System.out.println();
            }

            //判断输赢
            for(int j = 0;j<broad.length;j++){
                //左右
                if(broad[j][0] == broad[j][1] && broad[j][1] == broad[j][2]){
                    if(broad[j][1] == 'X'){
                        System.out.println("A胜");
                        return;
                    }else if(broad[j][1] == 'O'){
                        System.out.println("B胜");
                        return;
                    }
                }
                //上下
                if(broad[0][j] == broad[1][j] && broad[1][j] == broad[2][j]){
                    if(broad[1][j] == 'X'){
                        System.out.println("A胜");
                        return;
                    }else if(broad[1][j] == 'O'){
                        System.out.println("B胜");
                        return;
                    }
                }
            }
            //左上右下
            if(broad[0][0] == broad[1][1] && broad[1][1] == broad[2][2]){
                if(broad[1][1] == 'X'){
                    System.out.println("A胜");
                    return;
                }else if(broad[1][1] == 'O'){
                    System.out.println("B胜");
                    return;
                }
            }
            //左下右上
            if(broad[0][2] == broad[1][1] && broad[1][1] == broad[2][0]){
                if(broad[1][1] == 'X'){
                    System.out.println("A胜");
                    return;
                }else if(broad[1][1] == 'O'){
                    System.out.println("B胜");
                    return;
                }
            }
        }
    }


    //五子棋
    public static void work2_12(){
        Scanner scanner = new Scanner(System.in);
        char[][] broad = new char[15][15];
        for (int i = 0;i<broad.length;i++){
            for (int j = 0;j<broad[i].length;j++){;
                broad[i][j] = ' ';
            }
        }

        //创建游戏
        for(int i = 0;i< 225;i++){
            System.out.println("您要下在第几列：");
            int column = scanner.nextInt()-1;
            if(column >= 15){
                System.out.println("棋盘只有15列");
                continue;
            }
            System.out.println("您要下在第几行：");
            int line = scanner.nextInt()-1;
            if(line >= 15){
                System.out.println("棋盘只有15行");
                continue;
            }
            if(broad[line][column] == ' '){
                broad[line][column] = i % 2 == 0?'X':'O';
            }else {
                System.out.println("这个位置已经下过");
                i -= 1;
            }

            //遍历
            for (int a = 0;a<broad.length;a++){
                for (int b = 0;b<broad[a].length;b++){
                    System.out.print(broad[a][b] + "\t");
                }
                System.out.println();
            }

            //判断输赢
            //左右
            for(int l = 0;l<broad.length;l++){
                for(int j = 4;j<broad[l].length;j++){
                    char ch = broad[l][j];
                    if(ch == ' '){
                        continue;
                    }
                    if (ch == broad[l][j-1] && ch == broad[l][j-2] &&  ch == broad[l][j-3] &&  ch == broad[l][j-4]){
                        if (ch == 'X'){
                            System.out.println("A赢得了比赛");
                            return;
                        }else {
                            System.out.println("B赢得了比赛");
                            return;
                        }
                    }
                }
            }
            //上下
            for(int l = 0;l<broad.length;l++) {
                for (int j = 4; j < broad[l].length; j++) {
                    char ch = broad[j][l];
                    if (ch == ' ') {
                        continue;
                    }
                    if (ch == broad[j-1][l] && ch == broad[j-2][l] &&  ch == broad[j-3][l] &&  ch == broad[j-4][l]){
                        if (ch == 'X'){
                            System.out.println("A赢得了比赛");
                            return;
                        }else {
                            System.out.println("B赢得了比赛");
                            return;
                        }
                    }
                }
            }
            //左上右下
            for(int l = 4;l< broad.length;l++){
                for (int j = 4;j<broad[l].length;j++){
                    char ch = broad[l][j];
                    if(ch == ' '){
                        continue;
                    }
                    if(ch == broad[l-1][j-1] && ch == broad[l-2][j-2] &&  ch == broad[l-3][j-3] &&  ch == broad[l-4][j-4]){
                        if (ch == 'X'){
                            System.out.println("A赢得了比赛");
                            return;
                        }else {
                            System.out.println("B赢得了比赛");
                            return;
                        }
                    }
                }
            }
            //右上左下
            for(int l = 4;l< broad.length;l++) {
                for (int j = broad[l].length - 4; j >= 0; j--) {
                    char ch = broad[l][j];
                    if (ch == ' ') {
                        continue;
                    }
                    if(ch == broad[l-1][j + 1] && ch == broad[l-2][j+2] &&  ch == broad[l-3][j + 3] &&  ch == broad[l-4][j + 4]){
                        if (ch == 'X'){
                            System.out.println("A赢得了比赛");
                            return;
                        }else {
                            System.out.println("B赢得了比赛");
                            return;
                        }
                    }
                }
            }
        }
    }
}
class Student2_6 implements Comparable{
    private String name;
    private int age;
    private int grade;

    public Student2_6(String name, int age, int grade) {
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getGrade() {
        return grade;
    }

    @Override
    public String toString() {
        return "Student2_6{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", grade=" + grade +
                '}' + "\n";
    }

    @Override
    public int compareTo(Object o) {
        Student2_6 s = (Student2_6) o;
        if(this.age > s.age){
            return -1;
        } else if (this.age < s.age) {
            return 1;
        }else {
            if(this.grade > s.grade){
                return -1;
            } else if (this.grade < s.grade) {
                return 1;
            }else {
                return 0;
            }
        }
    }
}

class Score implements Comparable{
    private static int id = 0;
    private int grade;

    public Score() {
        id++;
        grade = (int)(Math.random()*100);
    }

    public int getId() {
        return id;
    }

    public int getGrade() {
        return grade;
    }

    @Override
    public String toString() {
        return "Score{" +
                "id=" + id +
                ", grade=" + grade +
                '}' + "\n";
    }

    @Override
    public int compareTo(Object o) {
        Score score = (Score) o;
        if(this.grade > score.grade){
            return 1;
        } else if (this.grade <score.grade) {
            return -1;
        }else {
            return 0;
        }
    }
}

